package com.wushi.lenovo.asia5b.app_di.credit.presenter;

import android.content.Context;
import android.graphics.Bitmap;

import com.mvp.base.BasePresenter;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.CreditTransferActivityView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_utils.helper.CrashHelper;

import java.io.File;
import java.io.FileOutputStream;

import javax.inject.Inject;

import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;


/**
 * Created by zhangyuncai on 2019/1/26.
 */
public class CreditTransferActivityPresenter extends BasePresenter<CreditTransferActivityView> {

    ApiService apiService;

    @Inject
    public CreditTransferActivityPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void getImageByBitmap(Context context, String email) {
        Disposable subscribe = Observable.just(email)
                .map(new Function<String, Bitmap>() {
                    @Override
                    public Bitmap apply(String s) throws Exception {
                        return QRCodeEncoder.syncEncodeQRCode(s, BGAQRCodeUtil.dp2px(context, 150));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    getView().showLoadingBar("progress");
                })
                .doAfterTerminate(() -> {
                    getView().dismissLoadingBar("");
                })
                .subscribe(bitmap -> {
                    if (bitmap != null) {
                        Timber.d("--->>>>>>>>---setImageByBitmap");
                        getView().getImageByBitmapResult(bitmap);
                    } else {
                        CrashHelper.postCatchedException(new Throwable("邮箱地址转bitmap失败!"));
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
        composite.add(subscribe);
    }

    public void getImagePathByCode(Bitmap mBitmap, File file,boolean isShare) {

        if (mBitmap != null) {
            Disposable subscribe = Observable.just(mBitmap)
                    .map(new Function<Bitmap, String>() {
                        @Override
                        public String apply(Bitmap bitmap) throws Exception {
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                                    new FileOutputStream(file));
                            return file.getAbsolutePath();
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(disposable -> {
                        getView().showLoadingBar(null);
                    })
                    .doAfterTerminate(() -> {
                        getView().dismissLoadingBar("");
                    })
                    .subscribe(imgPath -> {
                        getView().getImagePathByCodeResult(imgPath,isShare);
                    }, throwable -> {
                        throwable.printStackTrace();
                        getView().getImagePathByCodeResult(null,isShare);
                    });
            composite.add(subscribe);
        }

    }
}